#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <errno.h>  
#include <unistd.h>  
  
#include <netdb.h>  
#include <net/if.h>  
#include <arpa/inet.h>  
#include <sys/ioctl.h>  
#include <sys/types.h>  
#include <sys/socket.h> 

#include <time.h>

#include "ssd1306.h"
#include "fonts.h"

const unsigned char qrcode[]={
        0xFE,0xE0,0x98,0x73,0xF8,0x82,0xDC,0x5B,0xF2,0x08,0xBA,0x06,0xA8,0x3A,0xE8,0xBA,
        0x98,0x0E,0x1A,0xE8,0xBA,0xF4,0xD1,0xFA,0xE8,0x82,0xDD,0x62,0x5A,0x08,0xFE,0xAA,
        0xAA,0xAB,0xF8,0x00,0x39,0xE3,0xA0,0x00,0x12,0x0C,0xE2,0x51,0xD8,0x68,0x62,0x1D,
        0xE2,0x78,0xC6,0xC8,0xA9,0x0A,0xA8,0xE5,0x82,0x42,0xD1,0x88,0xD3,0x8D,0x2A,0x53,
        0x68,0x90,0x37,0xD0,0x53,0x58,0xAF,0x88,0x01,0x6F,0xA8,0xA4,0x28,0x01,0xF1,0x10,
        0x52,0x00,0x00,0x86,0x60,0x48,0xA4,0x00,0xAE,0xE8,0xAB,0xF4,0x00,0x0A,0x78,0xF1,
        0xCC,0x01,0xA7,0xA8,0x9A,0x08,0x00,0x3B,0x90,0x04,0x40,0x00,0x84,0x30,0xD6,0x98,
        0x00,0xEF,0xF8,0x5C,0x9F,0xB4,0x4C,0x88,0x13,0x83,0xD1,0x96,0x68,0x79,0x5D,0x27,
        0xAB,0x68,0xBA,0xCE,0xFA,0xA4,0x88,0x75,0xEA,0x70,0xEF,0x08,0xDA,0x59,0xCB,0x0F,
        0xF0,0x00,0xCC,0x87,0xA8,0x98,0xFE,0x64,0xD0,0x9A,0xB8,0x82,0x38,0x47,0xB8,0xA0,
        0xBA,0x3C,0xB8,0xCF,0xD0,0xBA,0x8C,0x51,0x55,0xB8,0xBA,0x20,0x8A,0xD8,0xA8,0x82,
        0x66,0xE4,0x59,0xC0,0xFE,0x6F,0x44,0x8E,0x18,
};// 37x37

const unsigned char logo[]={
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x1F,0xF8,0x00,0x7F,0x00,0x00,0x00,0x00,0x7E,0x00,0x1F,0xFC,0x00,0x00,
        0x00,0x00,0x7F,0xFE,0x00,0x7F,0xFC,0x00,0x00,0x3F,0xFE,0x00,0x7F,0xFF,0x00,0x00,
        0x00,0x01,0xFF,0xFF,0x80,0x7F,0xFF,0x80,0x01,0xFF,0xFE,0x01,0xFF,0xFF,0x80,0x00,
        0x00,0x01,0xFF,0xFF,0xC0,0x7F,0xFF,0xE0,0x07,0xFF,0xFE,0x01,0xFF,0xFF,0xC0,0x00,
        0x00,0x03,0xFF,0xFF,0xC0,0x7F,0xFF,0xF8,0x1F,0xFF,0xFE,0x03,0xFC,0x3F,0xE0,0x00,
        0x00,0x07,0xF8,0x1F,0xE0,0x7F,0xFF,0xFC,0x3F,0xF0,0x3E,0x07,0xE0,0x07,0xF0,0x00,
        0x00,0x0F,0xC0,0x07,0xF0,0x7C,0x03,0xFE,0x7F,0x00,0x3E,0x0F,0xC0,0x03,0xF0,0x00,
        0x00,0x00,0x00,0x03,0xF0,0x7C,0x00,0x7F,0xFC,0x00,0x3E,0x0F,0xC0,0x01,0xF8,0x00,
        0x00,0x00,0x00,0x01,0xF8,0x7C,0x00,0x3F,0xF8,0x00,0x3E,0x1F,0x80,0x00,0xF8,0x00,
        0x00,0x1E,0x00,0x03,0xF8,0x7C,0x00,0x1F,0xF0,0x00,0x3E,0x1F,0x00,0x00,0x7C,0x00,
        0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xE0,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,
        0x00,0x1F,0xFF,0xFF,0xFF,0xFF,0xE0,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,
        0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xE0,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,
        0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xE0,0x03,0xE0,0x07,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,
        0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x07,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,
        0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x07,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,
        0x00,0x1F,0x00,0x00,0x78,0x7C,0x00,0x07,0xF0,0x07,0xFF,0xFF,0xFF,0xFF,0xFC,0x00,
        0x00,0x1F,0x00,0x00,0xF8,0x7C,0x00,0x1F,0xF8,0x00,0x3E,0x1F,0x80,0x00,0x00,0x00,
        0x00,0x1F,0x80,0x01,0xF8,0x7C,0x00,0x3F,0xFE,0x00,0x3E,0x0F,0xC0,0x00,0x00,0x00,
        0x00,0x0F,0xC0,0x03,0xF0,0x7C,0x00,0x7F,0x7F,0x00,0x3E,0x0F,0xE0,0x01,0xF0,0x00,
        0x00,0x0F,0xE0,0x07,0xF0,0x7C,0x03,0xFE,0x7F,0xF0,0x3E,0x07,0xF0,0x07,0xF0,0x00,
        0x00,0x07,0xF0,0x0F,0xE0,0x7C,0xFF,0xF8,0x1F,0xFF,0xFE,0x07,0xFE,0x3F,0xE0,0x00,
        0x00,0x03,0xFF,0xFF,0xC0,0x7F,0xFF,0xE0,0x0F,0xFF,0xFE,0x03,0xFF,0xFF,0xC0,0x00,
        0x00,0x01,0xFF,0xFF,0x80,0x7F,0xFF,0xC0,0x03,0xFF,0xFE,0x01,0xFF,0xFF,0x80,0x00,
        0x00,0x00,0xFF,0xFF,0x00,0x7F,0xFE,0x00,0x00,0x7F,0xFE,0x00,0x7F,0xFF,0x00,0x00,
        0x00,0x00,0x3F,0xFE,0x00,0x7F,0xE0,0x00,0x00,0x07,0xFE,0x00,0x3F,0xFC,0x00,0x00,
        0x00,0x00,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};// 128x64

int get_local_ip(const char *eth_inf, char *ip)
{
    int sd;
    struct sockaddr_in sin;
    struct ifreq ifr;

    sd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sd) {
        printf("socket error: %s\n", strerror(errno));
        return -1;
    }

    strncpy(ifr.ifr_name, eth_inf, IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ - 1] = 0;

    // if error: No such device
    if (ioctl(sd, SIOCGIFADDR, &ifr) < 0) {
        printf("ioctl error: %s\n", strerror(errno));
        close(sd);
        return -1;
    }

    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    snprintf(ip, 16, "%s", inet_ntoa(sin.sin_addr));

    close(sd);

    return 0;
}

int main(void)
{
    time_t time_now;
    struct tm *time_local; 
    char time_string[32];
    char clock_string[16];
    char ip_string[16];  
    const char *eth = "eth0";  
    unsigned int i = 0;

    ssd1306_init();

    while (1) {
        ssd1306_clear_screen(0x00);
        ssd1306_draw_bitmap(0, 0, logo, 128, 64);
        ssd1306_refresh_gram();
        usleep(5000000);
        
        ssd1306_clear_screen(0x00);
        ssd1306_draw_bitmap(49, 16, qrcode, 37, 37);
        ssd1306_refresh_gram();
        usleep(5000000);
        
        i = 500;
        while (--i) {
            get_local_ip(eth, ip_string);  
            time(&time_now);
            time_local = localtime(&time_now);
            sprintf(time_string, "%s", asctime(time_local));
            strncpy(clock_string, time_string + 11, 8);
            clock_string[8] = '\0';
            ssd1306_clear_screen(0x00);
            ssd1306_display_string(32-strlen(ip_string), 0, ip_string, FONT_1608, NORMAL);
            ssd1306_display_string(0, 16, clock_string, FONT_3216, NORMAL);
            ssd1306_refresh_gram();
            usleep(100000);
        }
    }
}

